package edu.ccut.saturn.studio.wfg.dialog.userdialog;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.dialogs.TitleAreaDialog;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.Tree;

//import edu.ccut.saturn.sqlutil.DBDataUitl;
import edu.ccut.saturn.studio.Activator;
import edu.ccut.saturn.studio.Images;
import edu.ccut.saturn.studio.core.util.eclipse.Messages;
import edu.ccut.saturn.studio.database.model.DBConnection;
import edu.ccut.saturn.studio.database.model.DBConnectionProvider;
import edu.ccut.saturn.studio.wfg.dialog.SelectionCandidate;
import edu.ccut.saturn.studio.wfg.model.Shape;
import edu.ccut.saturn.studio.wfg.wfgmodel.UsersAttribute;

public class UserEditNew extends TitleAreaDialog {
	
	private Text IDText, nameText;
	
	private Combo typeCombo;
	
	private Label IDLabel, nameLabel, typeLabel;
	
	private UsersAttribute usersAttribute;
	
	private TreeViewer treeViewer;
	
	private Tree tree; 
	
	private Role  roleTree;
	
	private User userTree;
	
	private Department departmentTree;
	
	private Position positionTree;
	
	private DBConnection dbconnection = null;
	
	private String dbname = "";
	
	private Map<String, String> dbInformationMap;

	public UserEditNew(Shell parent, UsersAttribute usersAttribute, Shape shape, String dbname) {
		super(parent);
		this.usersAttribute = usersAttribute;
		this.dbconnection = DBConnectionProvider.getInstance().getConnection(
				dbname);
		this.dbname = dbname;
		dbInformationMap = iniMap(shape,dbname);
	}
	
	private Map<String,String> iniMap(Shape shape,String dbname){
		Map<String,String> map = new HashMap<String,String>();
		dbconnection = DBConnectionProvider.getInstance().getConnection(dbname);
						
						map.put("driverClassName",dbconnection.getDriverClassName());
						map.put("password",dbconnection.getPassword());
						map.put("url",dbconnection.getUrl());
						map.put("userid",dbconnection.getUsername());
		
		return map;
	}
	
	@Override
	protected Control createContents(Composite parent) {
		Control contents = super.createContents(parent);
		setMessage(Messages.getString("UserEditNew.SetAttribute"));   //$NON-NLS-1$
		return contents;
	}
	
	@Override
	protected Control createDialogArea(Composite parent) {
		getShell().setText(SelectionCandidate.ATTRIBUTE_SETTING);
		setTitleImage(Activator.getImage(Images.SATURN_ICONS_MAXNAME));
		
		Composite area = (Composite) super.createDialogArea(parent);
		final Composite composite = new Composite(area, SWT.NONE);
		
		final GridData gd_composite = new GridData(SWT.FILL, SWT.FILL, true, true);
		composite.setLayoutData(gd_composite);
		final GridLayout gridLayout = new GridLayout();
		gridLayout.numColumns = 2;
		composite.setLayout(gridLayout);

		final Group group = new Group(composite, SWT.NONE);
		group.setLayout(new FormLayout());
		final GridData gd_group = new GridData(SWT.FILL, SWT.FILL, true, false, 2, 1);
		gd_group.heightHint = 161;
		gd_group.horizontalIndent = 3;
		gd_group.verticalIndent = 10;
		group.setLayoutData(gd_group);

		treeViewer = new TreeViewer(group, SWT.BORDER);
		treeViewer.setUseHashlookup(true);
		tree = treeViewer.getTree();
		final FormData fd_tree = new FormData();
		fd_tree.bottom = new FormAttachment(100, -5);
		fd_tree.top = new FormAttachment(0, 12);
		fd_tree.right = new FormAttachment(100, -5);
		fd_tree.left = new FormAttachment(0, 3);
		tree.setLayoutData(fd_tree);
		treeViewer.setContentProvider(new TreeViewerContentProvider());
		treeViewer.setLabelProvider(new TreeViewerLabelProvider());
		
		List<OrgnazitionTree> input = DataFactory.createTreeData(dbInformationMap,dbconnection);
		if(input!=null){
			treeViewer.setInput(input);
		}
		
		tree.addMouseListener(new MouseAdapter(){
			public void mouseDown(MouseEvent e){
			
				IStructuredSelection selection = (IStructuredSelection)treeViewer.getSelection();
				if(selection.getFirstElement() instanceof Role){
					roleTree = (Role)selection.getFirstElement();
					IDText.setText(roleTree.getId().trim());
					nameText.setText(roleTree.getName().trim());
					typeCombo.setText(roleTree.getType().trim());
				} else if(selection.getFirstElement() instanceof User){
					userTree = (User)selection.getFirstElement();
					IDText.setText(userTree.getId().trim());
					nameText.setText(userTree.getName().trim());
					typeCombo.setText(userTree.getType().trim());
				} else if(selection.getFirstElement() instanceof Department){
					departmentTree = (Department)selection.getFirstElement();
					IDText.setText(departmentTree.getId().trim());
					nameText.setText(departmentTree.getName().trim());
					typeCombo.setText(departmentTree.getType().trim());
				} else if(selection.getFirstElement() instanceof Position){
					positionTree = (Position)selection.getFirstElement();
					IDText.setText(positionTree.getId().trim());
					nameText.setText(positionTree.getName().trim());
					typeCombo.setText(positionTree.getType().trim());
				} 
			}			
		});
		
		IDLabel = new Label(composite, SWT.NONE);
		final GridData gd_iDLabel = new GridData();
		gd_iDLabel.verticalIndent = 8;
		gd_iDLabel.horizontalIndent = 2;
		IDLabel.setLayoutData(gd_iDLabel);
		IDLabel.setText("ID:"); 

		IDText = new Text(composite, SWT.BORDER);
		IDText.setFocus();
		final GridData gd_iDText = new GridData(SWT.FILL, SWT.CENTER, true, false);
		gd_iDText.verticalIndent = 8;
		gd_iDText.horizontalIndent = 2;
		IDText.setLayoutData(gd_iDText);

		nameLabel = new Label(composite, SWT.NONE);
		final GridData gd_nameLabel = new GridData();
		gd_nameLabel.verticalIndent = 8;
		gd_nameLabel.horizontalIndent = 2;
		nameLabel.setLayoutData(gd_nameLabel);
		nameLabel.setText(Messages.getString("UserEditNew.Name"));   //$NON-NLS-1$

		nameText = new Text(composite, SWT.BORDER);
		final GridData gd_nameText = new GridData(SWT.FILL, SWT.CENTER, true, false);
		gd_nameText.verticalIndent = 8;
		gd_nameText.horizontalIndent = 2;
		nameText.setLayoutData(gd_nameText);

		typeLabel = new Label(composite, SWT.NONE);
		final GridData gd_typeLabel = new GridData();
		gd_typeLabel.verticalIndent = 8;
		gd_typeLabel.horizontalIndent = 2;
		typeLabel.setLayoutData(gd_typeLabel);
		typeLabel.setText(Messages.getString("UserEditNew.Type"));   //$NON-NLS-1$

		typeCombo = new Combo(composite, SWT.BORDER | SWT.READ_ONLY);
		final GridData gd_typeCombo = new GridData(SWT.FILL, SWT.CENTER, true, false);
		gd_typeCombo.verticalIndent = 8;
		gd_typeCombo.horizontalIndent = 2;
		typeCombo.setLayoutData(gd_typeCombo);
		typeCombo.setItems(SelectionCandidate.userTypes);
		
		IDText.setText(usersAttribute.getId().trim());
		nameText.setText(usersAttribute.getName().trim());
		typeCombo.setText(usersAttribute.getType());
		
		return area;
	}
	
	@Override
	protected void buttonPressed(int buttonId) {
		if(buttonId == IDialogConstants.OK_ID){
			usersAttribute.setId(IDText.getText().trim());
			usersAttribute.setName(nameText.getText().trim());
			usersAttribute.setType(typeCombo.getText().trim());
		}
		super.buttonPressed(buttonId);
	}
	
	@Override
	protected Point getInitialSize() {
		return new Point(600,450);
	}
	protected void configureShell(Shell newShell) {
		super.configureShell(newShell);
		newShell.setImage(Activator.getImage(Images.SATURN_ICONS_SMALLNAME));
	}

	public String getDbname() {
		return dbname;
	}

	public void setDbname(String dbname) {
		this.dbname = dbname;
	}
}
